import { Selector } from '@ngxs/store';
import { DemoState, DemoStateModel } from './demo.state';
import { RouterState, RouterStateModel } from '@ngxs/router-plugin';
import { RouterStateParams } from '../router';
import * as _ from 'lodash';

export class DemoSelector {
  @Selector([DemoState])
  static editFlag({editFlag}: DemoStateModel) {
    return editFlag;
  }

  @Selector([DemoState])
  static editType({editType}: DemoStateModel) {
    return editType;
  }

  @Selector([DemoState])
  static menuEditFlag({menuEditFlag}: DemoStateModel) {
    return menuEditFlag;
  }

  @Selector([DemoState])
  static menuEditType({menuEditType}: DemoStateModel) {
    return menuEditType;
  }

  @Selector([DemoState])
  static list({list}: DemoStateModel) {
    return list;
  }

  @Selector([DemoState, RouterState])
  static current({list}: DemoStateModel, {state}: RouterStateModel<RouterStateParams>) {
    let url = state?.url;
    let result = url?.replace(/\/demo\/(.*)/, '$1');
    return _.find(list, s => s.code === result)?.list;
  }

  @Selector([DemoState])
  static menuCodeList({list}: DemoStateModel) {
    return _.map(list, (s: any) => {
      return {
        id: s.id,
        code: s.code,
      };
    });
  }

}
